package com.hengpeng.itfinoss.controller.userLevelSet;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.en.LevelType;
import com.hengpeng.itfinbase.en.UserLevel;
import com.hengpeng.itfinbase.persist.UserLevelSet;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.util.PageHandlerUtil;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * 会员等级
 * 
 * @author liyangyang
 *
 */
@Controller
@RequestMapping(value = "/userLevelSet")
public class UserLevelSetController extends BaseController {
    @Autowired
    private ServiceFactory serviceFactory;

    private String jspPath(String pageName) {

        return "userLevelSet/" + pageName;
    }

    @RequestMapping(value = "/load/{id}")
    public String ossConfig(ModelMap map, HttpServletRequest request, @PathVariable Long id) {
        UserLevelSet userLevelSet = serviceFactory.getPersistService().get(UserLevelSet.class, id);
        map.put("userLevelSet", userLevelSet);
        return jspPath("/detail");
    }

    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, ModelMap map, UserLevel userLevel, LevelType levelType) {

        Pagination page = new Pagination(request);
        page.setPageSize(this.getPageSize(request));
        map.put("userLevel", userLevel);
        map.put("levelType", levelType);
        try {
            List<UserLevelSet> list = new ArrayList<UserLevelSet>();
            ServiceResult<List<UserLevelSet>> result = serviceFactory.getUserLevelSetSrervice().selectUserLevelSetPage(
                    userLevel, levelType, page.currentPage, page.pageSize);
            page.setEntityCount(result.getReturnListSize());
            list = result.getReturnValue();
            map.put("userLevelSetList", list);
            map.put("userLevelSetListPage", page);
            return "/userLevelSet/list";
        } catch (Exception ex) {
            ex.printStackTrace();
            return returnError(map, ex);
        }
    }

    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, @ModelAttribute UserLevelSet userLevelSet, ModelMap map,
            HttpSession session) {
        try {
            userLevelSet.setCreateTime((serviceFactory.getPersistService()
                    .get(UserLevelSet.class, userLevelSet.getId()).getCreateTime()));
            userLevelSet.setUpdateTime(new Date());
            serviceFactory.getUserLevelSetSrervice().updateUserLevelSetById(userLevelSet);
        } catch (Exception e) {
            e.printStackTrace();
            return PageHandlerUtil.returnError(map, e);
        }
        return PageHandlerUtil.returnSuccess(map, "/userLevelSet/list");
    }

    @RequestMapping(value = "/create")
    public String initSave(ModelMap map) {
        map.put("userLevelSet", new UserLevelSet());
        return "userLevelSet/detail";
    }

    @RequestMapping(value = "/onsave")
    public String save(@ModelAttribute UserLevelSet userLevelSet, ModelMap map, HttpSession session) {
        try {
            userLevelSet.setCreateTime(new Date());
            serviceFactory.getPersistService().save(userLevelSet);
            return PageHandlerUtil.returnSuccess(map, "/userLevelSet/list");
        } catch (Exception ex) {
            ex.printStackTrace();
            return PageHandlerUtil.returnError(map, ex);
        }
    }

    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, @ModelAttribute UserLevelSet userLevelSet, ModelMap map) {
        try {
            serviceFactory.getPersistService().delete(UserLevelSet.class, userLevelSet.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return PageHandlerUtil.returnError(map, e);
        }
        return PageHandlerUtil.returnSuccess(map, "/userLevelSet/list");
    }

}
